package com.ktgj.sms.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.api.RemoteCustomerService;
import com.ktgj.customer.api.domain.Department;
import com.ktgj.customer.api.domain.Employee;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.sms.mapper.SmsTemplateMapper;
import com.ktgj.sms.domain.SmsTemplate;
import com.ktgj.sms.service.ISmsTemplateService;

import javax.servlet.http.HttpServletRequest;

/**
 * 短信模板Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-11
 */
@Service
public class SmsTemplateServiceImpl implements ISmsTemplateService 
{
    @Autowired
    private SmsTemplateMapper smsTemplateMapper;
    @Autowired
    private RemoteCustomerService remoteCustomerService;

    /**
     * 查询短信模板
     * 
     * @param smsTemplateId 短信模板ID
     * @return 短信模板
     */
    @Override
    public SmsTemplate selectSmsTemplateById(Long smsTemplateId) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SmsTemplate smsTemplate = smsTemplateMapper.selectSmsTemplateById(smsTemplateId);
        if (smsTemplate != null){
            Map<String, Object> map = new HashMap<>();
            map.put("id", smsTemplate.getEmployeeId());
            Employee employee = remoteCustomerService.employeeNameList(map);
            if (employee != null){
                smsTemplate.setEmployeeName(employee.getEmployeeName());
            }

            Map<String, Object> objectMap = new HashMap<>();
            objectMap.put("id", smsTemplate.getDeptId());
            Department department = remoteCustomerService.deptName(objectMap);
            if (department != null){
                smsTemplate.setDeptName(department.getDeptName());
            }

            if (smsTemplate.getCreateTime() != null){
                smsTemplate.setCreationTime(format.format(smsTemplate.getCreateTime()));
            }
        }
        return smsTemplate;
    }

    @Override
    public SmsTemplate querySmsTemplateById(Long smsTemplateId) {
        return smsTemplateMapper.selectSmsTemplateById(smsTemplateId);
    }

    /**
     * 查询短信模板列表
     * 
     * @param smsTemplate 短信模板
     * @return 短信模板
     */
    @Override
    public List<SmsTemplate> selectSmsTemplateList(SmsTemplate smsTemplate)
    {
        return smsTemplateMapper.selectSmsTemplateList(smsTemplate);
    }

    /**
     * 新增短信模板
     * 
     * @param smsTemplate 短信模板
     * @return 结果
     */
    @Override
    public int insertSmsTemplate(SmsTemplate smsTemplate, HttpServletRequest request) throws Exception {
        String token = request.getHeader("token");
        Employee employee = remoteCustomerService.getLoginEmployeeByToken(token).getData();
        if (employee != null){
            smsTemplate.setEmployeeId(employee.getEmployeeId());
        }
        else {
            throw new Exception("登录已过期");
        }
        smsTemplate.setCreateTime(new Date());
        smsTemplate.setIsDelete("false");
        int i = smsTemplateMapper.insertSmsTemplate(smsTemplate);
        return i;
    }

    /**
     * 修改短信模板
     * 
     * @param smsTemplate 短信模板
     * @return 结果
     */
    @Override
    public int updateSmsTemplate(SmsTemplate smsTemplate)
    {
        return smsTemplateMapper.updateSmsTemplate(smsTemplate);
    }

    /**
     * 批量删除短信模板
     * 
     * @param smsTemplateIds 需要删除的短信模板ID
     * @return 结果
     */
    @Override
    public int deleteSmsTemplateByIds(Long[] smsTemplateIds)
    {
        return smsTemplateMapper.deleteSmsTemplateByIds(smsTemplateIds);
    }

    /**
     * 删除短信模板信息
     * 
     * @param smsTemplateId 短信模板ID
     * @return 结果
     */
    @Override
    public int deleteSmsTemplateById(Long smsTemplateId)
    {
        return smsTemplateMapper.deleteSmsTemplateById(smsTemplateId);
    }

    /**
     * 短信模板列表
     * */
    @Override
    public List<SmsTemplate> smsTemplateList(SmsTemplate smsTemplate, HttpServletRequest request) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String token = request.getHeader("token");
        Employee employee = remoteCustomerService.getLoginEmployeeByToken(token).getData();

        if (employee == null){
            if (smsTemplate.getEmployeeType() != null && smsTemplate.getEmployeeType().equals("true")){
                throw new Exception("登录已过期");
            }
        }
        else {
            smsTemplate.setEmployeeId(employee.getEmployeeId());
        }

        List<SmsTemplate> smsTemplateList = smsTemplateMapper.smsTemplateList(smsTemplate);
        if (smsTemplateList != null && smsTemplateList.size() > 0){
            for (SmsTemplate template : smsTemplateList) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", template.getEmployeeId());
                employee = remoteCustomerService.employeeNameList(map);
                if (employee != null){
                    template.setEmployeeName(employee.getEmployeeName());
                }

                Map<String, Object> objectMap = new HashMap<>();
                objectMap.put("id", template.getDeptId());
                Department department = remoteCustomerService.deptName(objectMap);
                if (department != null){
                    template.setDeptName(department.getDeptName());
                }

                if (template.getCreateTime() != null){
                    template.setCreationTime(format.format(template.getCreateTime()));
                }
            }
        }
        return smsTemplateList;
    }
}
